
import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    GET_INVENTORY_COMMON_EXPRESS_PLATFORM,
    GET_WAREHOUSE_ORDER_PROCESS,
    EDIT_WAREHOUSE_ORDER_READY,
    EDIT_WAREHOUSE_ORDER_MISS,
    EDIT_WAREHOUSE_ORDER_PICKUP,
    GET_WAREHOUSE_ORDER_INFO,
    EDIT_WAREHOUSE_ORDER_DELIVER,
    GET_INVENTORY_SALES_LIST
} from "@constants/ActionTypes";
import {getAxios, postAxios, putAxios, deleteAxios} from "@utils/api";

// 查看可选配送平台列表
export const getInventoryCommonExpressPlatform = (params, callback) => ({
    type: apiType(GET_INVENTORY_COMMON_EXPRESS_PLATFORM).request,
    params,
    callback
})
// 查看待处理履约单列表
export const getWarehouseOrderProcess = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_ORDER_PROCESS).request,
    params,
    callback
})
// 订单备货完成
export const creatWarehouseOrderReady = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE_ORDER_READY).request,
    params,
    callback
})
// 订单缺货通知
export const creatWarehouseOrderMiss = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE_ORDER_MISS).request,
    params,
    callback
})
// 订单自提
export const creatWarehouseOrderPickUp = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE_ORDER_PICKUP).request,
    params,
    callback
})
// 查看订单详情
export const getWarehouseOrderInfo = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_ORDER_INFO).request,
    params,
    callback
})
// 订单发货
export const creatWarehouseOrderDeliver = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE_ORDER_DELIVER).request,
    params,
    callback
})
// 经销存报表列表查看
export const getInventorySalesList = (params, callback) => ({
    type: apiType(GET_INVENTORY_SALES_LIST).request,
    params,
    callback
})

const sagas = {
    * [apiType(GET_INVENTORY_COMMON_EXPRESS_PLATFORM).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/common/express/platform', params)
            yield put({ type: apiType(GET_INVENTORY_COMMON_EXPRESS_PLATFORM).success, response });
            callback && callback(response.platforms)
        } catch (e) {
            yield put({ type: apiType(GET_INVENTORY_COMMON_EXPRESS_PLATFORM).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_ORDER_PROCESS).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/order/processing', params)
            yield put({ type: apiType(GET_WAREHOUSE_ORDER_PROCESS).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_ORDER_PROCESS).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE_ORDER_READY).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/order/ready', params)
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDER_READY).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDER_READY).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE_ORDER_MISS).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/order/lack/notify', params)
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDER_MISS).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDER_MISS).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE_ORDER_PICKUP).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/order/pick-up', params)
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDER_PICKUP).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDER_PICKUP).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_ORDER_INFO).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/order/info', params)
            yield put({ type: apiType(GET_WAREHOUSE_ORDER_INFO).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_ORDER_INFO).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE_ORDER_DELIVER).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/order/deliver', params)
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDER_DELIVER).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDER_DELIVER).failure});
        }
    },
    * [apiType(GET_INVENTORY_SALES_LIST).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/stats/month-sales/list', params)
            yield put({ type: apiType(GET_INVENTORY_SALES_LIST).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_INVENTORY_SALES_LIST).failure});
        }
    },
}
export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})